package com.wang.system.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.StringValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import net.sf.jsqlparser.expression.Expression;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author wang
 * @Date 2024/2/14 18:06
 * @PackageName:com.atguigu.system.config
 * @ClassName: MybatisPlusConfig
 * @Description: TODO
 * @Version 1.0
 */
@Configuration
@MapperScan("com.wang.**.mapper")
@Slf4j
public class MybatisPlusConfig {
    /**
     * 分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor addPaginationInnerInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //向Mybatis过滤器链中添加分页拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加多租户插件
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                // 动态判断是否是注册接口，避免租户ID
                if (isRegisterRequest()) {
                    return null; // 在注册请求时跳过tenant_id注入
                }
                // 从上下文中获取当前租户ID
                String tenantId = TenantContext.getTenantId();
                return new StringValue(tenantId);
            }

            @Override
            public boolean ignoreTable(String tableName) {
                // 指定哪些表不需要加租户ID
                return "sys_tenant".equals(tableName);  // 例如租户表本身不需要租户ID
            }

            // 判断是否是注册接口的方法
            private boolean isRegisterRequest() {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes != null) {
                    HttpServletRequest request = attributes.getRequest();
                    String requestURI = request.getRequestURI();
                    log.info("当前请求路径：{}", requestURI);
                    return requestURI != null && requestURI.contains("/register");
                }
                return false;
            }
        }));
        return interceptor;
    }
}
